﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace Ocmf.Types
{
    public static class LocalSettings
    {
        public static string ApplicationDataFolder { get; set; }
        public static string ApplicationImageFolder { get { return Path.Combine(ApplicationDataFolder, "Images"); } }
        public static string ApplicationExperimentFolder { get { return Path.Combine(ApplicationDataFolder, "Experiments"); } }
        public static string ApplicationSessionFolder { get { return Path.Combine(ApplicationDataFolder, "Sessions"); } }
        public static string ApplicationScreenTemplateFolder { get { return Path.Combine(ApplicationDataFolder, "Screen Templates"); } }
        public static int ChamberExplorerRefreshInterval { get; set; }
        public static bool DbOnline { get; set; }
        public static string DbServer { get; set; }
        public static string DbInstance { get; set; }
        public static string DbPassword { get; set; }
        public static string DbUsername { get; set; }
        public static string FileExtensionExperiment { get; set; }
        public static string FileExtensionSession { get; set; }
        public static string FileExtensionScreenTemplate { get; set; }

        static LocalSettings()
        {
            ApplicationDataFolder = (string) Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("ApplicationDataFolder");
            ChamberExplorerRefreshInterval = int.Parse((string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("ChamberExplorerRefreshInterval"));
            DbOnline = bool.Parse((string) Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("DbOnline"));
            DbServer = (string) Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("DbServer");
            DbInstance = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("DbInstance");
            DbPassword = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("DbPassword");
            DbUsername = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("DbUsername");
            FileExtensionExperiment = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("FileExtensionExperiment");
            FileExtensionSession = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("FileExtensionSession");
            FileExtensionScreenTemplate = (string)Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).GetValue("FileExtensionScreenTemplate");
        }

        public static void SaveToRegistry()
        {
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("ApplicationDataFolder",ApplicationDataFolder);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("ChamberExplorerRefreshInterval", ChamberExplorerRefreshInterval.ToString());
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("DbOnline", DbOnline.ToString());
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("DbServer", DbServer);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("DbInstance", DbInstance);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("DbPassword", DbPassword);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("DbUsername", DbUsername);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("FileExtensionExperiment", FileExtensionExperiment);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("FileExtensionSession", FileExtensionSession);
            Registry.CurrentUser.OpenSubKey(@"Software\OCMF", true).SetValue("FileExtensionSession", FileExtensionScreenTemplate);
        }
    }
}
